using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;

namespace ClientGUI
{
    class DisplayProperties:Control
    {
        Module my_current;
        ListView my_attrview;
        TextBox my_edtbox;
        ListViewItem my_selected_item;

        Label my_current_item_lbl;

        public DisplayProperties()
            :base()
        {
            my_current = null;
            my_attrview = new ListView();
            my_edtbox = new TextBox();
            my_edtbox.Hide();
            my_edtbox.KeyPress += new KeyPressEventHandler(my_edtbox_KeyPress);
            my_edtbox.BorderStyle = BorderStyle.None;

            InitializeList();

            my_current_item_lbl = new Label();
            Controls.Add(my_current_item_lbl);
            my_current_item_lbl.Left = 0;
            my_current_item_lbl.Top = 0;
            my_current_item_lbl.Width = Width;
            my_current_item_lbl.Height += 2;
            my_current_item_lbl.Show();
            my_current_item_lbl.Font = new System.Drawing.Font("Sans Serif", 14.0f,
                                                    System.Drawing.FontStyle.Bold);
            my_current_item_lbl.BackColor = System.Drawing.Color.White;
            my_current_item_lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            my_attrview.Top = my_current_item_lbl.Height;

            my_attrview.Click += new EventHandler(my_attrview_Click);
            my_attrview.MultiSelect = false;
            my_attrview.FullRowSelect = true;

            my_attrview.MultiSelect = false;
            my_attrview.FullRowSelect = true;
            my_attrview.View = View.Details;
            my_attrview.ShowGroups = false;
            my_attrview.MultiSelect = false;
            //my_attrview.HideSelection = false;
            my_attrview.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            my_attrview.GridLines = true;


            Resize += new EventHandler(DisplayProperties_Resize);

            Controls.Add(my_attrview);
        }

        void my_attrview_Click(object sender, EventArgs e)
        {
            my_edtbox.Focus();
        }

        void DisplayProperties_Resize(object sender, EventArgs e)
        {
            my_attrview.Height = Height - my_current_item_lbl.Height;
            my_attrview.Width = Width;
            my_current_item_lbl.Width = Width;
        }

        public void Use(Module m)
        {
            if (m == null)
            {
                my_current_item_lbl.Text = "<none>";
                my_edtbox.Text = "";
                my_edtbox.Hide();
                my_attrview.Items.Clear();
                my_selected_item = null;
                return;
            }

            Console.WriteLine("Displaying Properties");
            my_current = m;
            display();
            my_current_item_lbl.Text = "<" + m.Name + ">";
            my_edtbox.Text = "";
            my_edtbox.Hide();
        }


        private void InitializeList()
        {
            my_attrview.View = View.Details;
            my_attrview.LabelEdit = true;
            my_attrview.GridLines = true;

            ColumnHeader prop_name = new ColumnHeader();
            prop_name.Text = "Name";
            prop_name.Width = 73;
            prop_name.DisplayIndex = 0;
            prop_name.Name = "Name";

            ColumnHeader prop_value = new ColumnHeader();
            prop_value.Text = "Value";
            prop_value.Width = 73;
            prop_value.DisplayIndex = 1;
            prop_value.Name = "Value";

            my_attrview.Columns.Clear();
            my_attrview.Columns.Add(prop_name);
            my_attrview.Columns.Add(prop_value);

            my_attrview.SelectedIndexChanged += new EventHandler(my_view_SelectedIndexChanged);
        }

        #region EDITING
        void my_view_SelectedIndexChanged(object sender, EventArgs e)
        {
            my_attrview.Controls.Add(my_edtbox);
            if (my_attrview.SelectedItems.Count > 0)
            {
                ListViewItem selected = my_attrview.SelectedItems[0];
                int x = selected.SubItems[1].Bounds.X;
                int y = selected.SubItems[1].Bounds.Y;
                int w = selected.SubItems[1].Bounds.Width;
                int h = selected.SubItems[1].Bounds.Height;

                my_edtbox.Width = w - 4;
                my_edtbox.Height = h;
                my_edtbox.Top = y;
                my_edtbox.Left = x + 4;
                

                my_edtbox.Text = selected.SubItems[1].Text;
                my_selected_item = selected;

                my_edtbox.Show();
                my_edtbox.Focus();
                my_edtbox.SelectAll();
            }
        }
        void my_edtbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //return
            {
                my_selected_item.SubItems[1].Text = my_edtbox.Text;
                my_edtbox.Hide();

                my_current.SetAttribute(my_selected_item.SubItems[0].Text,
                                        my_edtbox.Text);
            }
            if (e.KeyChar == 37) //escape?
            {
                my_edtbox.Hide();
            }
        }

        #endregion
        /// <summary>
        /// Handles the actual display of properties for this item.
        /// </summary>
        private void display()
        {
            my_attrview.Items.Clear();
            TagConfig config = my_current.TagConfig;
            for (int i = 0;  i < config.AllowedAttributes.Length; ++i)
            {
                String [] item = new String[2];
                item[0] = config.AllowedAttributes[i];
                String val = my_current.GetAttribute(item[0]);
                if (val != null)
                    item[1] = val;
                else
                    item[1] = "<not set>";
                my_attrview.Items.Add(new ListViewItem(item));
            }
        }
    }
}
